<?php
class Post_model extends CI_Model{
	var $_tableName = 'data';
    function __construct(){
        // Call the Model constructor
        parent::__construct();
    }
    public function getLatestPost($offset = 0, $limit = '', $offsetId = 0){
    	if($limit == '') $limit = HOME_LIMIT;

    	$this->db->select('id, like_count, share_count, view, post_title, post_image, crawl_date, item_type, category_id, source_page, video_code');
        if($offsetId > 0){
            $this->db->where('id < ', $offsetId); 
        }
        $this->db->order_by("id", "desc");
    	$query = $this->db->get($this->_tableName, $limit, $offset);
        return $query->result();
    }
    public function getPostDetail($id)
    {
        $this->db->select('id, post_title, post_image, crawl_date, item_type, category_id, source_page, video_code');
        $query = $this->db->get_where($this->_tableName, array('id' => $id));
        return $query->result();
    }
    public function navigationPost($postId)
    {
        $sqlNav = 'SELECT id, post_title FROM '.$this->_tableName.' WHERE id = (SELECT MIN(id) FROM '.$this->_tableName.' where id > '.$postId.') OR id = (SELECT MAX(id) FROM '.$this->_tableName.' where id < '.$postId.')';
        $query = $this->db->query($sqlNav);
        return $query->result();
    }
    public function updatePostView($postId)
    {
        $sqlView = "UPDATE ".$this->_tableName." SET view = view + 1 WHERE id='$postId'";
        $query = $this->db->query($sqlView); 
    }
    public function getPostByCate($cateId, $offset = 0, $limit = '', $offsetId = 0){
        if($limit == '') $limit = CATE_LIMIT;
        $this->db->select('id, view, like_count, share_count, post_title, post_image, crawl_date, item_type, category_id, source_page, video_code');
        $this->db->where('category_id', $cateId); 
        if($offsetId > 0){
            $this->db->where('id < ', $offsetId); 
        }
        $this->db->order_by("id", "desc");

        $query = $this->db->get($this->_tableName, $limit, $offset);
        return $query->result();
    }
    public function getOlderPost($currentId, $limit = 9, $offset = 0)
    {
        $this->db->select('id, post_title, post_image');
        $this->db->where('id <', $currentId);
        $this->db->order_by("id", "desc");
        $query = $this->db->get($this->_tableName, $limit, $offset);
        return $query->result();
    }
}